//
//  HomeHeaderView.swift
//  Community
//
//  Created by MAC on 2019/8/6.
//  Copyright © 2019 ZKJ. All rights reserved.
//

import UIKit

class HomeHeaderView: UIView,NibLoadable {

    let cotentW: CGFloat = kScreenW - 30
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var contentView: UIView!
    var selected:((Int)->Void)?
    override func awakeFromNib() {
        super.awakeFromNib()
        bgView.backgroundColor = mainGreenColor
        contentView.layer.cornerRadius = 10
        contentView.layer.masksToBounds = true
        contentView.backgroundColor = .white//mainGreenColor.withAlphaComponent(0.2)
        let firstColor = UIColor.white.cgColor
        let secColor = mainGreenColor.withAlphaComponent(0.2).cgColor
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [firstColor,secColor]
        gradientLayer.locations = [0,0.75]
        gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint.init(x: 0, y: 1.0)
        gradientLayer.frame = CGRect.init(x: 0, y: 0, width: self.width, height: self.height)
        contentView.layer.insertSublayer(gradientLayer, at: 0)
        
        let images = ["fenlei","zhishi"]
        let titles = [NSLocalizedString("分类", comment: ""),NSLocalizedString("知识", comment: "")]
        var lastBtn:UIButton? = nil
        for (idx,item) in images.enumerated() {
            let btn = UIButton.init(type: .custom)
            self.contentView.addSubview(btn)
            btn.tag = idx
            btn.addBlock(for: .touchUpInside) { [weak self] (sender) in
                self?.selected?(btn.tag)
            }
            
            
            let imageV = UIImageView.init(image: UIImage(named: item))
            imageV.contentMode = .scaleAspectFit
            btn.addSubview(imageV)
            imageV.snp.makeConstraints { (make) in
                make.left.right.equalToSuperview()
                make.top.equalToSuperview().offset(20)
                make.height.equalTo(60)
            }
            
            let label = UILabel()
            label.text = titles[idx]
            label.font = constant.kFontSize(x: 15)
            label.textAlignment = .center
            btn.addSubview(label)
            label.snp.makeConstraints { (make) in
                make.left.right.equalToSuperview()
                make.top.equalTo(imageV.snp.bottom)
            }
            
            btn.snp.makeConstraints { (make) in
                if lastBtn == nil {
                     make.left.equalToSuperview()
                }else {
                     make.left.equalTo(lastBtn!.snp.right)
                }
                make.width.equalTo(cotentW / CGFloat(images.count))
                make.top.bottom.equalToSuperview()
            }
            lastBtn = btn
        }
        
    }

}
